package com.mytest.aspect;

import com.mytest.annotation.TestAnno;
import com.mytest.controller.TestController;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.aop.support.AopUtils;
import org.springframework.stereotype.Component;

/**
 * @author Wulc
 * @date 2024/4/7 14:11
 * @description
 */
@Aspect
@Component
public class LogAdvice {
    @Pointcut("execution(* com.mytest.service.impl.*.*(..))")
    private void logServicePointcut() {

    }

    @Pointcut("execution(* com.mytest.controller.*.*(..))")
    private void logControllerPointcut() {

    }

    @Pointcut("@annotation(com.mytest.annotation.TestAnno)")
    private void logAnnotationPointcut() {

    }

    @After("logServicePointcut()")
    public void logService() {
        System.out.println("After112233");
    }

    @Before("logControllerPointcut()")
    public void logController() {
        System.out.println("beforeABC");
    }

    @Before("logAnnotationPointcut()")
    public void logAnnotation() throws ClassNotFoundException, NoSuchMethodException {
        System.out.println("Annotation");
        //获取变量上的注解
        //获取类上的注解
        //获取方法上的注解value
        TestAnno testAnno = TestController.class.getMethod("test1").getAnnotation(TestAnno.class);
        System.out.println(testAnno.value());
        //获取加了@MyAnnotation_2注解的类的对象（AnnotationTest04）
        //Class c = Class.forName("com.mytest.annotation.TestAnno");
        //System.out.println(c);
        //判断类上面是否有 @MyAnnotation_2注解
        //if (c.isAnnotationPresent(TestAnno.class)) {
//        if (c.isAnnotation()) {
//            //获取注解对象
//            TestAnno testAnno = (TestAnno) c.getAnnotation(TestAnno.class);
//            System.out.println(testAnno);
//            String value = testAnno.value();  //获取注解对象属性
//            System.out.println("注解的类的对象的value属性值为：" + value);
//        }
//        TestAnno testAnno =
//                AopUtils.findAnnotation(target, ActionAccessControl.class);
    }
}
